package com.tuijian.service.fineranking;

import com.tuijian.service.rearrange.PostRearRange;
import com.tuijian.service.rearrange.ProductRearRange;
import com.tuijian.vo.Post;
import com.tuijian.vo.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.*;
import java.util.Comparator;

/**
 * @author Liu JianDong
 * @create 2023-06-08-14:48
 **/
@Service
public class FineRankingService {


    @Resource
    private ProductRearRange productRearRange;

    @Resource
    private PostRearRange postRearRange;


    public void fineRanking(String userId, String type, Map<String,Double> map) throws ItemTypeMismatchException {
        List<Map.Entry<String, Double>> entryList = new ArrayList<Map.Entry<String, Double>>(map.entrySet());
        Collections.sort(entryList, new Comparator<Map.Entry<String, Double>>() {
            @Override
            public int compare(Map.Entry<String, Double> me1, Map.Entry<String, Double> me2) {
                return me1.getValue().compareTo(me2.getValue()); // 升序排序
                //return me2.getValue().compareTo(me1.getValue()); // 降序排序
            }
        });

        //TODO:把排完序的商品列表和用户id传入模型得到新的排序结果

        // LinkedList<String> itemList = new LinkedList<String>();
        // itemList.add("1001-1-2-3-4-5");
        // itemList.add("1002-3-4-3-4-5");
        // String cmd = "python D:\\Notepad\\test.py " + userId +" "+itemList;
        // String arr = "";
        // try {
        //     Process process = Runtime.getRuntime().exec(cmd);
        //     InputStream inputStream = process.getInputStream();
        //     BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
        //     String line = null;
        //     while ((line = reader.readLine()) != null) {
        //         arr+=line+"\r\n";
        //     }
        // } catch (
        //         IOException e) {
        //     e.printStackTrace();
        // }
        // System.out.println(arr);

        //假设这就是排完序的结果
        double[] rankedScore = new double[map.size()];

        for (int i = 0; i < rankedScore.length; i++) {
            rankedScore[i] = new Random().nextDouble();
        }

        Map<String, Double> rankedMap = new HashMap<>();

        for (int i = 0; i < entryList.size(); i++) {
            rankedMap.put(entryList.get(i).getKey(), rankedScore[i]);
        }



        if (ItemType.POST.getType().equals(type)) {
            //调用帖子方法
            List<Post> old_sort = new ArrayList<>();
            for (int i = 0; i < entryList.size(); i++) {
                Post post = new Post();
                post.setPostId(entryList.get(i).getKey());
                post.setSort(i);
                post.setScore1(new Random().nextDouble());
                post.setScore2(new Random().nextDouble());
                old_sort.add(post);
            }
            postRearRange.rearRange(old_sort, userId);
        } else if (ItemType.PRODUCT.getType().equals(type)) {
            List<Product> old_sort = new ArrayList<>();
            for (int i = 0; i < entryList.size(); i++) {
                Product product = new Product();
                product.setProductId(entryList.get(i).getKey());
                product.setScore(entryList.get(i).getValue());
                product.setUserId(userId);
                old_sort.add(product);
            }
            productRearRange.rearRange(old_sort,0.85);
        }else {
            throw new ItemTypeMismatchException("类型只能是0或1，对应帖子和商品");

        }
    }
}
